﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using FormControllers;
using AgencyEntityLib.BaseEntity;
using Ecopsy.Tools;
using Ecopsy.DataTier;


public partial class iFarmeDictionary_AdressPrograms : FormAdressPrograms
{
    private wsDictionary _ws_Dictionary = (wsDictionary)GlobalConstants.CurrentAgency.WS_FactoryList[WS_FactoryList.wsKey_Dictionary];

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            MyBind();

        }
    }   

    protected void AddButton_Click(object sender, EventArgs e)
    {
        // если нет справочника - создать его
        wsDictionary _ws_Dictionary = (wsDictionary)GlobalConstants.CurrentAgency.WS_FactoryList[WS_FactoryList.wsKey_Dictionary];
        TreeDictionaryItem d = _ws_Dictionary.CreateTreeDictionary(GlobalConstants.TreeDictionaryFolderType_System, GlobalConstants.TreeDictionary_AdressProgram, "Новая адресная программа");
        Response.Redirect("~\\iFarmeDictionary_TreeDictonary.aspx?TreeDictionaryID=" + d.ID + "&FolderID=" + GlobalConstants.TreeDictionaryFolderType_System);  
    }
   
    protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
    {   
     
      MyBind();
    }
  
    #region Private
    private string TreeDictionaryType { get { return Request.QueryString["TreeDictionaryType"]; } }

    private TreeDictionaryItem[] GetLvList(object where)
    {
        TreeDictionaryItem[] r = GetList(Where);

        // фильтруем по нам=именованию
        var result =
          from
              n in r
          where
            n.Name.ToUpper().IndexOf(txtSearch.Text.ToUpper()) > -1 || txtSearch.Text.Length == 0
          select n;

        TreeDictionaryItem[] rr = result.ToArray<TreeDictionaryItem>();
        Array.Sort(rr, Where.SortCondition);
        return rr;

    }

    private void MyBind()
    {
        ObjectDS_F ODS = new ObjectDS_F(GetLvList, Where);
        grdEmployeers.DataSource = ODS.ImAsObjectDataSource;
        DataBind();
        SetSortCss();
    }

  
    
   

    private string FolderID
    {
        get { return GlobalConstants.TreeDictionaryFolderType_System; }
    }

    private string DictionaryTypeID
    {
        get { return GlobalConstants.TreeDictionary_AdressProgram ; }
    }

    #endregion

    
    protected void SetSortCss()
    {
        if (null == grdEmployeers.HeaderRow) return;

        foreach (TableCell cell in grdEmployeers.HeaderRow.Cells) cell.CssClass = string.Empty;
        string css = (Where.SortCondition.SortDirection == SortDirection.Ascending ? "sortAscending" : "sortDescending");

        switch (Where.SortCondition.SortField)
        {
            case "Name":
                grdEmployeers.HeaderRow.Cells[0].CssClass = css;
                break;
            case "CreateDate":
                grdEmployeers.HeaderRow.Cells[1].CssClass = css;
                break;
            case "LastUpdate":
                grdEmployeers.HeaderRow.Cells[2].CssClass = css;
                break;
        }


    }

    protected void grdEmployeers_Sorting(object sender, GridViewSortEventArgs e)
    {
        Where.SortCondition.SortDirection = (Where.SortCondition.SortDirection == SortDirection.Ascending ? SortDirection.Descending : SortDirection.Ascending); ;
        Where.SortCondition.SortField = e.SortExpression;
        SetSortCss();
        MyBind();

    }
 
    protected void grdEmployeers_RowCommand(object sender, GridViewCommandEventArgs e)
    {

    }
    
    protected void BtnEdit_Click(object sender, EventArgs e)
    {
        Response.Redirect("~\\iFarmeDictionary_TreeDictonary.aspx?TreeDictionaryID=" + ((Button)sender).CommandArgument + "&FolderID=" + GlobalConstants.TreeDictionaryFolderType_System);  

    }
}
